//
//  FascinatingController.m
//  HappyBaby
//
//  Created by 华哥 on 15/11/23.
//  Copyright © 2015年 华哥. All rights reserved.
//

#import "FascinatingController.h"
#import "FancinatingTableCell.h"
#import "FancinatingModel.h"
#import "CheckMoreController.h"
@interface FascinatingController () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *modelArray;


@end


@implementation FascinatingController

- (NSMutableArray *)modelArray
{
    if (_modelArray == nil) {
        self.modelArray = [NSMutableArray array];
    }
    return _modelArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"趣学";
    
    // 注册cell
    [self.tableView registerClass:[FancinatingTableCell class] forCellReuseIdentifier:@"FancinatingCell"];
    
    // 创建请求链接:http://api.bbwansha.com:8080/v22/index.php/Home/theme?userid=0&page=1
    NSURL *url = [NSURL URLWithString:@"http://api.bbwansha.com:8080/v22/index.php/Home/theme?userid=0&page=1"];
    [[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        // 解析数据
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = [dic objectForKey:@"data"];
        for (NSDictionary *modelDic in array) {
            FancinatingModel *model = [[FancinatingModel alloc] init];
            [model setValuesForKeysWithDictionary:modelDic];
            [self.modelArray addObject:model];
        }
        dispatch_sync(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    }] resume];
    
    
    
}
// 返回分区下行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FancinatingTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FancinatingCell" forIndexPath:indexPath];
    FancinatingModel *model = self.modelArray[indexPath.row];
    cell.model = model;
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CheckMoreController *checkMoreVc = [[CheckMoreController alloc] init];
    checkMoreVc.hidesBottomBarWhenPushed = YES;
    
    checkMoreVc.fancinatingModel = self.modelArray[indexPath.row];
    [self.navigationController pushViewController:checkMoreVc animated:YES];
}

@end
